#include <QtGui>
#include "cellEdit.h"

CellEdit::CellEdit(QWidget *parent) : QWidget(parent) {

  init();
  
} //CellEdit::CellEdit(QWidget *parent) : QWidget(parent)

CellEdit::CellEdit(QColor color, QWidget *parent) : QWidget(parent) {

  init();
  setColor(color);

} //CellEdit::CellEdit(QWidget *parent = 0, QColor color) : QWidget(parent)

void CellEdit::init() {

  this->resize(WCELLEDIT, HCELLEDIT);

  intSpin = new QSpinBox(this);
  intSpin->resize(WCELLEDIT-WBUTTON, HCELLEDIT);
  intSpin->move(0, 0);
  intSpin->show();
  connect(intSpin, SIGNAL(valueChanged(int)), this, SLOT(emChangeValueISpin(int)));
  connect(intSpin, SIGNAL(valueChanged(int)), this, SLOT(setLabelI()));

  doubleSpin = new QDoubleSpinBox(this);
  doubleSpin->setDecimals(DEFPRECISION);
  doubleSpin->resize(WCELLEDIT-WBUTTON, HCELLEDIT);
  doubleSpin->move(0, 0);
  doubleSpin->hide();
  connect(doubleSpin, SIGNAL(valueChanged(double)), this, SLOT(emChangeValueDSpin(double)));
  connect(doubleSpin, SIGNAL(valueChanged(double)), this, SLOT(setLabelD()));

  button = new QPushButton(this);
  button->setText(DEFTEXTBUTTON);
  button->resize(WBUTTON, HCELLEDIT);
  button->move(WCELLEDIT-WBUTTON, 0);
  button->show();
  connect(button, SIGNAL(clicked()), this, SLOT(emClickButton()));

  indent = 25;

  label = new QLabel(this);
  label->setOpenExternalLinks(false);
  label->setTextInteractionFlags(Qt::NoTextInteraction);
  label->setAutoFillBackground(true);
  label->setText("");
  label->resize(WCELLEDIT-WBUTTON, HCELLEDIT);
  label->move(0, 0);
  label->hide();
  isIndent(true);
  // setColor(QColor(246, 246, 246));

  setLabelColorForSpin(false);
  setIsIntSpin(DEFISINTSPIN);
  setIsShowSpin(DEFISSHOWSPIN);
  setIsShowButton(DEFISSHOWBUT);

  setIMin(DEFIMIN);
  setIMax(DEFIMAX);
  setDMin(DEFDMIN);
  setDMax(DEFDMAX);

} //void CellEdit::init()

void CellEdit::setLabelColorForSpin(bool newVal) {

  insLabelColorForSpin = newVal;

  if (insLabelColorForSpin) {

    setColor(label->palette().base().color());

  } //if (insLabelColorForSpin)

  else {

    palette.setColor(QPalette::Base, QColor(Qt::white));
    intSpin->setPalette(palette);
    doubleSpin->setPalette(palette);

  } //else: if (insLabelColorForSpin)

} //void CellEdit::setLabelColorForSpin(bool newVal)

void CellEdit::isIndent(bool val) {

  if (val) {

    label->setIndent(indent);

  } //if (val)
  else {

    label->setIndent(0);
    label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);

  } //else: if (val)

} //void CellEdit::isIndent(bool val)

void CellEdit::setIsIntSpin(bool newVal) {

  insIsIntSpin = newVal;
  
  if (newVal) {

    doubleSpin->hide();
    intSpin->show();

  } //if (newVal)
  else {

    intSpin->hide();
    doubleSpin->show();

  } //else: if (newVal)

} //void CellEdit::setIsIntSpin(bool newVal)

void CellEdit::setIsShowSpin(bool newVal) {

  insIsShowSpin = newVal;

  if (newVal) {

    label->hide();
    if (insIsIntSpin)
      intSpin->show();
    else
      doubleSpin->show();

  } //if (newVal)

  else {

    label->show();
    intSpin->hide();
    doubleSpin->hide();

  } //else: if (newVal)

} //void CellEdit::setIsShowSpin(bool newVal)
  
void CellEdit::setIsShowButton(bool newVal) {

  insIsShowButton = newVal;
  if (newVal) {
    intSpin->resize(WCELLEDIT-WBUTTON, HCELLEDIT);
    doubleSpin->resize(WCELLEDIT-WBUTTON, HCELLEDIT);
    label->resize(WCELLEDIT-WBUTTON, HCELLEDIT);
    button->show();
  } //if (newVal)
  else {
    intSpin->resize(WCELLEDIT, HCELLEDIT);
    doubleSpin->resize(WCELLEDIT, HCELLEDIT);
    label->resize(WCELLEDIT, HCELLEDIT);
    button->hide();
  } //else: if (newVal)

} //void CellEdit::setIsShowButton(bool newVal)

void CellEdit::setEditView() {

  if (insIsShowButton) {

    button->show();
    button->move(this->width()-WBUTTON, 0);

    label->resize(this->width() - WBUTTON, this->height());
    intSpin->resize(this->width() - WBUTTON, this->height());
    doubleSpin->resize(this->width() - WBUTTON, this->height());

  } //if (insIsShowButton)

  else {

    button->hide();
    
    label->resize(this->width(), this->height());
    intSpin->resize(this->width(), this->height());
    doubleSpin->resize(this->width(), this->height());

  } //else: if (insIsShowButton)

  if (isShowSpin()) {

    if (insIsIntSpin) {

      intSpin->show();
      intSpin->setFocus();

    } //if (insIsIntSpin)

    else {

      doubleSpin->show();
      doubleSpin->setFocus();

    } //else: if (insIsIntSpin)

    label->hide();

  } //if (isShowSpin())

  else {

    intSpin->hide();
    doubleSpin->hide();
    label->show();
    button->setFocus();
    
  } //else: if (isShowSpin())

} //void CellEdit::setEditView()

void CellEdit::setLabelView() {

  button->hide();
  intSpin->hide();
  doubleSpin->hide();

  label->show();
  label->resize(WCELLEDIT, HCELLEDIT);

} //void CellEdit::setLabelView()

void CellEdit::setColor(const QColor & newColor) { 
  
  palette.setColor(QPalette::Base, newColor); 
  palette.setColor(QPalette::Window, newColor);
  label->setPalette(palette);
  if (insLabelColorForSpin) {

    intSpin->setPalette(palette);
    doubleSpin->setPalette(palette);

  } //if (insLabelColorForSpin)

} //void CellEdit::setColor(const QColor & newColor)

void CellEdit::setAddStr(const QString & newVal) { 

  insAddStr = newVal; 
  //Обновление label
  if (insIsIntSpin)
    setLabelI();
  else
    setLabelD();

} //void CellEdit::setAddStr(const QString & newVal)

void CellEdit::setLabelI() {

  QString str("");
  str += QString().setNum(valueIntSpin());
  str += insAddStr;
  label->setText(str);

} //void CellEdit::setLabelI()

void CellEdit::setLabelD() {

  QString str("");
  str += QString().setNum(toPrec(valueDoubleSpin()), 'g', STRPREC);
  str += insAddStr;
  label->setText(str);

} //void CellEdit::setLabelD()

bool CellEdit::isNull() {

  if (isIntSpin()) {

    if (valueIntSpin() == 0)
      return true;

  } //if (isIntSpin())

  else {

    if (valueDoubleSpin() < 0.0001)
      return true;

  } //else: if (isIntSpin())

  return false;

} //bool CellEdit::isNull()

// void CellEdit::mousePressEvent(QMouseEvent *event) {
  
//   this->clearFocus();

// } //void CellEdit::mousePressEvent(QMouseEvent *event)

// void CellEdit::keyPressEvent(QKeyEvent * event) {

//   if (event->key() == Qt::Key_Escape) {

//     this->clearFocus();

//   } //else if (event->key() == Qt::Key_Return)

//   else if (event->key() == Qt::Key_Return) {

//     this->clearFocus();

//   } //else if (event->key() == Qt::Key_Return)

// } //void CellEdit::keyPressEvent(QKeyEvent * event)
